<?php
namespace app\service;
use think\facade\Db;
define("USERNAME","");//
define("PASSWORD","");//  d3ltajE2NDAzNw==

class iotmcuService{

	public function login(){
		
		//$url="https://wxapp.hzfc.cn/fgfw/wymjapitest/wymjapi/login?username=".USERNAME."&password=".PASSWORD;
		$url="https://wxapp.hzfc.cn/fgfw/wymjapi/wymjapi/login?username=".USERNAME."&password=".PASSWORD;
		$res=file_get_contents($url);
		return $res;
	}
	private function getToken(){
		
		$webinfo=Db::table("webset")->where("id","1")->find();
		if(time()-$webinfo["XAuthToken_time"]>60*60*8){
			$res=$this->login();
			$re=json_decode($res,true);
			Db::table("webset")->where("id","1")->update(["XAuthToken"=>$re["data"]["XAuthToken"],"XAuthToken_time"=>time()]);
			return $re["data"]["XAuthToken"];
		}else{
			return $webinfo["XAuthToken"];
		}
		
		
	}
	//企业下小区列表查询接口
	public function getXmcode(){
		//$url="https://wxapp.hzfc.cn/fgfw/wymjapitest/wymjapi/wymjapi-s1/getXmcode";
		$token=$this->getToken();
		$url="https://wxapp.hzfc.cn/fgfw/wymjapi/wymjapi/wymjapi-s1/getXmcode";
		//da8867f1-8cfd-4640-b000-16f8fbdd58ce
		$post_data = array(
          	"XAuthToken"=>$token
        );
		$result=$this->curl_post1($url,$post_data);
		return $result;
	}
	//门禁系统人员 身份认证接口 
	/*
	$xmcode：小区编码  $fwzl:房屋坐落 $xm 姓名 $sfzh:身份证号 $sjhm:手机号码 $cqrxm:产权人姓名 cqrsfzh:产权人身份证号  sflb 0：业主  1：家属    2：租户    3：访客   4：企业员工   5：物业员工
	*/
	public function sfrz($xmcode,$fwzl,$xm,$sfzh,$sjhm,$cqrxm,$cqrsfzh,$sflb){
		//$url="https://wxapp.hzfc.cn/fgfw/wymjapitest/wymjapi/wymjapi-s1/sfrz";
		$token=$this->getToken();
		$url="https://wxapp.hzfc.cn/fgfw/wymjapi/wymjapi/wymjapi-s1/sfrz";
		$post_data = array(
            "xmcode" => $xmcode,
            "fwzl" => $fwzl,
            "xm" => $xm,
            "sfzh" => $sfzh,
            "sjhm" => $sjhm,
            "cqrxm" => $cqrxm,
            "cqrsfzh" => $cqrsfzh,
            "sflb" => $sflb,
            "XAuthToken"=>$token
        );
       	$data=$this->curl_post1($url,$post_data);
       	return $data;
	}
	//人员健康码获取接口
	public function getJkmztByRyid($xmcode,$ryid,$sfzh,$yylx){
		$token=$this->getToken();
		//$url="https://wxapp.hzfc.cn/fgfw/wymjapitest/wymjapi/wymjapi-s1/getJkmztByRyid";			
		$url="https://wxapp.hzfc.cn/fgfw/wymjapi/wymjapi/wymjapi-s1/getJkmztByRyid";
		$post_data = array(
            "xmcode" => $xmcode,
            "ryid" => $ryid,
            "sfzh" => $sfzh,
            "yylx" => $yylx,
            "XAuthToken"=>$token
        );
        //  06191c4a-392b-49c2-9ab9-fbaca446150f
        //$data = ['xmcode'=>$xmcode,'ryid'=>$ryid,'sfzh'=>$sfzh,'yylx'=>$yylx];
       	$data=$this->curl_post1($url,$post_data);
       	return $data;
	}

	//门禁系统人员身份注销接口
	public function sfzx($xmcode,$ryid){
		$token=$this->getToken();
		//$url="https://wxapp.hzfc.cn/fgfw/wymjapitest/wymjapi/wymjapi-s1/sfzx";
		$url="https://wxapp.hzfc.cn/fgfw/wymjapi/wymjapi/wymjapi-s1/sfzx";
		$post_data = array(
            "xmcode" => $xmcode,
            "ryid" => $ryid,
            "XAuthToken"=>$token
        );
        
       	$data=$this->curl_post1($url,$post_data);
       	return $data;
		
	}
		
	
	public function curl_post1($url,$post_data){
		
		$headers = array('Content-Type: application/x-www-form-urlencoded');
		$curl = curl_init(); // 启动一个CURL会话
		curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
		curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
		//curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
		curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
		curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data)); // Post提交的数据包
		curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
		curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect: '));
		curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		$result = curl_exec($curl); // 执行操作
		if (curl_errno($curl)) {
		    echo 'Errno'.curl_error($curl);//捕抓异常
		}
		curl_close($curl); // 关闭CURL会话
		return $result;
	}
	public function curl_post($url,$post_data){
		$curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 1);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
       	//curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        return $data;//json_decode($data, true);
	}
}